package com.ct.ah.lab.controller;

import com.ct.ah.lab.entity.Cart;
import com.ct.ah.lab.entity.Product;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.entity.vo.CartVo;
import com.ct.ah.lab.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/carts")
public class CartController {
    @Autowired
    private ICartService cartService;


    @RequestMapping("/create")
    @ResponseBody
//    public Map<String, Object> create(Cart cart, HttpServletRequest req) {
    public Map<String, Object> create(@RequestBody Map<String, Object> reqMap, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        HttpSession session = req.getSession();
        Cart cart = new Cart();

        if(!reqMap.isEmpty()){
            for(Map.Entry<String, Object> entry:reqMap.entrySet()){
                System.out.println(entry.getKey()+"--->"+entry.getValue());
            }
        }else {
            map.put("msg","request data not found!");
            return map;
        }

        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");

        } else {
            cart.setUid(user.getUid());
            cart.setPid(Integer.parseInt(reqMap.get("pid").toString()));
            cart.setNum(Integer.parseInt(reqMap.get("num").toString()));
            cart.setPrice(Long.parseLong(reqMap.get("price").toString()));
            cartService.save(cart, user);
            map.put("state", 1000);
        }

        return map;
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<CartVo> carts = cartService.listCart(user.getUid());
            map.put("state", 1000);
            map.put("data", carts);

        }
        return map;
    }

    @RequestMapping("/findByCids")
    @ResponseBody
    public Map<String, Object> findByCids(HttpServletRequest req, Integer[] cids) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<Integer> cidList = Arrays.asList(cids);
            List<CartVo> carts = cartService.listCartByCids(cidList);
            map.put("state", 1000);
            map.put("data", carts);
        }
        return map;
    }

    @RequestMapping("/changeNum")
    @ResponseBody
    public Map<String, Object> changeNum(HttpServletRequest req, Integer cid, Integer num) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            Boolean result = cartService.changeNum(cid, num, user);
            if (result) {
                map.put("state", 1000);
            } else {
                map.put("msg", "only 1 in cart");
            }
        }
        return map;
    }


    @RequestMapping("/remove")
    @ResponseBody
    public Map<String, Object> remove(HttpServletRequest req, Integer cid) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            cartService.removeByCid(cid);
            map.put("state", 1000);
        }
        return map;
    }

    @RequestMapping("/removeList")
    @ResponseBody
    public Map<String, Object> removeList(HttpServletRequest req, Integer[] cids) {
        Map<String, Object> map = new HashMap<>();

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            List<Integer> cidList = Arrays.asList(cids);
            cartService.removeByCidList(cidList);
            map.put("state", 1000);
        }
        return map;
    }

    @RequestMapping("/findCid")
    @ResponseBody
    public Map<String, Object> findCid(HttpServletRequest req, @RequestBody Map<String, Object> reqMap) {

        Map<String, Object> map = new HashMap<>();
        if(!reqMap.isEmpty()) {
            for (Map.Entry<String, Object> entry : reqMap.entrySet()) {
                System.out.println(entry.getKey() + "--->" + entry.getValue());
            }
        }else {
            System.out.println("reqMap is null!!!");
        }
//
        Integer pid = Integer.parseInt(reqMap.get("pid").toString());
        Integer cid = null;

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            map.put("msg", "not login");
        } else {
            Integer uid = user.getUid();
            System.out.println("pid: "+pid + " uid: "+uid);
            cid = cartService.findCid(pid, uid);
            map.put("state", 1000);
            map.put("data", cid);
        }
        return map;
    }
}
